package com.have.blog.admin.controller;

import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.ToDoDto;
import com.have.blog.admin.service.AdminTodoService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.*;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(value = "待办事项相关接口", tags = {"待办事项相关接口"})
@RequestMapping("/todo")
@Slf4j
public class AdminTodoController {
    @Autowired
    private AdminTodoService todoService;

    @ApiOperation(value = "获取代办事项列表", notes = "获取代办事项列表", response = String.class)
    @PostMapping("/getList")
    public Wrapper<CommonPage> getList(@Validated({GetList.class}) @RequestBody ToDoDto todoDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("执行获取待办事项列表");
        List<ToDoDto> list = todoService.getList(todoDto);
        return Wrapper.success(CommonPage.restPage(list));
    }

    @OperationLogger(value = "增加代办事项")
    @ApiOperation(value = "增加代办事项", notes = "增加代办事项", response = String.class)
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody ToDoDto todoDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return todoService.addTodo(todoDto);
    }

    @OperationLogger(value = "编辑代办事项")
    @ApiOperation(value = "编辑代办事项", notes = "编辑代办事项", response = String.class)
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody ToDoDto todoDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return todoService.editTodo(todoDto);
    }

    @OperationLogger(value = "删除代办事项")
    @ApiOperation(value = "删除代办事项", notes = "删除代办事项", response = String.class)
    @PostMapping("/delete")
    public Wrapper delete(@Validated({Delete.class}) @RequestBody ToDoDto todoDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return todoService.deleteTodo(todoDto);
    }

    @OperationLogger(value = "批量编辑代办事项")
    @ApiOperation(value = "批量编辑代办事项", notes = "批量编辑代办事项", response = String.class)
    @PostMapping("/toggleAll")
    public Wrapper toggleAll(List<Integer> uids) {

        // 参数校验
        return todoService.editBatchTodo(uids);
    }

}
